package com.offer;


import com.bean.TreeNode;
import com.utils.TreeUtils;

import java.util.ArrayList;

public class Test54 {

    public int kthLargest(TreeNode root, int k) {

        ArrayList<TreeNode> arrayList = new ArrayList<>();

        dfs(root, arrayList);

        return arrayList.get(arrayList.size() - k).val;
    }

    private void dfs(TreeNode root, ArrayList<TreeNode> arrayList) {
        if (root == null) {
            return;
        }

        dfs(root.left, arrayList);

        arrayList.add(root);

        dfs(root.right, arrayList);
    }

    public static void main(String[] args) {
        Test54 test = new Test54();

        TreeNode node = TreeUtils.buildTree("5,3,6,2,4,null,null,1");

        System.out.println(test.kthLargest(node, 3));
    }

}
